﻿using RabbitMQ.Client;
using System.Text;

var connFactory = new ConnectionFactory();
connFactory.HostName = "127.0.0.1";
connFactory.DispatchConsumersAsync = true;
string exChangeName = "exchange1"; //交换机名字

var connection = connFactory.CreateConnection(); //创建TCP
while (true)
{
    using (var channel = connection.CreateModel()) //创建信道
    {
        var prop= channel.CreateBasicProperties(); 
        prop.DeliveryMode = 2;  //持久化
        channel.ExchangeDeclare(exChangeName, "direct"); // 申明交换机
        byte[] bytes = Encoding.UTF8.GetBytes(DateTime.Now.ToString());
        // 发布消息
        channel.BasicPublish(exChangeName, routingKey: "key1", mandatory: true, basicProperties: prop, body: bytes);
        Console.WriteLine("ok" + DateTime.Now);
    }
    Thread.Sleep(1000);
}
